package com.qxsx.midi.activity.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/11/3.
 */
public class MessageActivity extends Activity {
    TextView tv_name;
    Button btn_see, btn_cancel;
    String data;
    String id, message, type, exam_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        Intent intent = getIntent();
        data = intent.getStringExtra("data");//得到data

        findById();
        try {
            JSONObject object = new JSONObject(data);
            message = object.getString("message");
            type = object.getString("type");

            if (type.toString().equals("1")) {
                id = object.getString("id");
                exam_id = object.getString("exam_id");
                btn_see.setVisibility(View.VISIBLE);
                btn_cancel.setVisibility(View.VISIBLE);
            } else {

                btn_see.setVisibility(View.GONE);
                btn_cancel.setVisibility(View.GONE);
            }
            tv_name.setText(message);
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    private void findById() {

        tv_name = (TextView) findViewById(R.id.tv_name);
        btn_see = (Button) findViewById(R.id.btn_see);
        btn_see.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                messgae(10);
                finish();
            }
        });
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                messgae(11);
                finish();
            }
        });
    }

    private void messgae(int i) {
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();

        try {
            object.put("id", id);
            object.put("exam_id", exam_id);
            object.put("state", i + "");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("515", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                Toast.makeText(MessageActivity.this, message.toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });


    }
}
